package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ResultMessage2;
import com.mofang.powerdekorhelper.model.ResultMessage3;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.MinePointView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/7/6 0006.
 */

public class MinePointPersenter extends BasePresent<MinePointView> {

    RetrofitSerives retrofitSerives;
    Call<ResultMessage3> isSignCall;
    Call<ResultMessage4> signCall;
    Call<ResultMessage2> totalPointCall;

    public void getTotalPoint(Integer sellerId) {
        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services + "selectUserManagerIntegral/");

        totalPointCall = retrofitSerives.getTotalPoint(sellerId);

        totalPointCall.enqueue(new Callback<ResultMessage2>() {
            @Override
            public void onResponse(Call<ResultMessage2> call, Response<ResultMessage2> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setTotalPoint(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage2> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getIsSign(Integer userId) {
        view.showProgress();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services + "selectUserManagerIsSing/");
        isSignCall = retrofitSerives.getIsSign(userId);
        isSignCall.enqueue(new Callback<ResultMessage3>() {
            @Override
            public void onResponse(Call<ResultMessage3> call, Response<ResultMessage3> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {

                        if (response.body().isResult()) {
                            view.setIsSign(true);
                        } else {
                            view.setIsSign(false);
                        }
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage3> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });


    }

    public void getSign(Integer userId) {

        view.showProgress();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services +"addUserManagerIsSing/");
        signCall = retrofitSerives.getSign(userId);
        signCall.enqueue(new Callback<ResultMessage4>() {
            @Override
            public void onResponse(Call<ResultMessage4> call, Response<ResultMessage4> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setSign(response.body());
                    } else {
                        view.onError(Constants.SIGN_FAIL_INFO);
                    }
                } else {
                    view.onError(Constants.SIGN_FAIL_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage4> call, Throwable t) {
                view.onError(Constants.SIGN_FAIL_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
